<?php

namespace App\Utility;

use GuzzleHttp\Client;


class StarPayUtility
{


    public function createTrade(string $order_no, string $amount, string $currency, string $payTypeCode, string $returnUrl)
    {

        $url = 'https://api.star-pay.vip/api/gateway/pay';
        $mer_no = '070027';
        $clientSecret = '31b5133cb13c1bae699c842af7a6adbe';
        /*if ($currency == 'IDR') {
            $mer_no = '1002680';
            $amount = bcmul($amount, 14954, 2);
            $clientSecret = '162d7a5130057d88796756213a8d6c46';
        }else if ($currency == 'VND') {
            $mer_no = '1002681';
            $clientSecret = '06da20d2eba55a4ecfc2d88843738c3d';
            $amount = bcmul($amount, 23450, 2);
        } else {
            return false;
        }*/

      
        $json=[
            "merchant_ref"=>$order_no,
            'product'=>'CPay',
            'amount'=>$amount,
            ];
        
        $params = [
            'merchant_no' => $mer_no,
            'timestamp'=>time(),
            'sign_type'=>'MD5',
            'params'=>json_encode($json)
        ];
        $params['sign'] = $this->generateSign($params, $clientSecret);
        //发起请求
        $client = new Client();
        $res = $client->request('POST', $url, [
            'json' => $params
        ]);
        return json_decode($res->getBody()->getContents(), true);
    }

    public function fundApplyCheck(string $order_no,string $currency)
    {
        if ($currency == 'IDR') {
            $mer_no = '1002680';
            $clientSecret = '162d7a5130057d88796756213a8d6c46';
        }else if ($currency == 'VND') {
            $mer_no = '1002681';
            $clientSecret = '06da20d2eba55a4ecfc2d88843738c3d';
        } else {
            return false;
        }
        $url = 'http://www.fast-pay.cc/gateway.aspx';
        $params = [
            'mer_no' => $mer_no,
            'order_no' => $order_no,
            'method' => 'fund.apply.check',
        ];
        $params['sign'] = $this->generateSign($params, $clientSecret);
        //发起请求
        $client = new Client();
        $res = $client->request('POST', $url, [
            'json' => $params
        ]);
        return json_decode($res->getBody()->getContents(), true);
    }


    public function fundApply(string $order_no,
                              string $amount,
                              string $currency,
                              string $acc_code,
                              string $acc_name,
                              string $acc_no,
                              string $returnUrl)
    {

        if ($currency == 'IDR') {
            $mer_no = '1002680';
            $clientSecret = '162d7a5130057d88796756213a8d6c46';
        }else if ($currency == 'VND') {
            $mer_no = '1002681';
            $clientSecret = '06da20d2eba55a4ecfc2d88843738c3d';
        } else {
            return false;
        }


        $url = 'http://www.fast-pay.cc/gateway.aspx';
        $params = [
            'mer_no' => $mer_no,
            'order_no' => $order_no,
            'order_amount' => $amount,
            'acc_code' => $acc_code,
            'acc_name' => $acc_name,
            'acc_no' => $acc_no,
            'currency' => $currency,
            'method' => 'fund.apply',
            'returnurl' => $returnUrl,
        ];
        $params['sign'] = $this->generateSign($params, $clientSecret);
        //发起请求
        $client = new Client();
        $res = $client->request('POST', $url, [
            'json' => $params
        ]);
        return json_decode($res->getBody()->getContents(), true);
    }


    /**
     * 生成签名秘钥
     * @param array $params 参数
     * @param string $clientSecret 应用密钥AppSecret
     * @return string
     */
    public function generateSign(array $params, string $clientSecret = ''): string
    {
       $signString=$params['merchant_no'].$params['params'].$params['sign_type'].$params['timestamp'];
        return md5($signString . $clientSecret);
    }


}
